#!/usr/bin/perl
#########################################
# NAME meterDB_spec.pl
# DESC:  defines the structure of the meters database.
#        
# HIST: 16mar2008  GJ  ORIGINAL VERSION
#
package meterDB_spec;

sub new 
{
    my $self = shift;
    my $class = ref($self) || $self;
    bless {},$self;
}

sub table
{
    my ($self,$table) = @_;
    my $func = sprintf('%s_%s','deftable',$table);

    my $tablespec = {};
    $tablespec = &{$func};
    return $tablespec;
}

sub table_list
{
    my $self = shift;
    my @tables = grep /^deftable_/,keys %MyDBSpec::;
    map {s/^deftable_//} @tables;
    return \@tables;
}

sub getfieldlist
{
    my ($self,$table)= @_;
    my $fields = [];
    my $tablespec = $self->table($table);
    foreach my $field (@{$tablespec->{fields}})
    {
        push @{$fields},$field->{field};
    }
    return $fields;
}

############################################################
sub deftable_meters {
    my $tablespec = {
       'description' => 'Meters to be monitored by the metermon daemon.',
       'fields' => [
		    {
			'opt' => 'auto_increment not null primary key',
			'type' => 'int',
			'field' => 'id',
		    },
		    {
			'field' => 'description',
			'type'  => char,
			'size'  => '20',
		    },
		    {
			'field' => 'unit',
			'type'  => char,
			'size'  => '10',
		    },
		    {
			'field' => 'cfac',
			'type'  => double,
			'size'  => '50',
		    },
		    {
			'field' => 'output_period',
			'type'  => int,
		    },
		    {
			'field' => 'iocard',
			'type'  => int,
		    },
		    {
			'field' => 'input',
			'type'  => int,
		    },
		    {
			'field' => 'reading',
			'type'  => double,
		    },
		    ],
    };
#    print "defined meters\n";
    return $tablespec;

}

############################################################
sub deftable_readings {
    my $tablespec = {
       'description' => 'Meter Readings',
       'fields' => [
		    {
			'opt' => 'auto_increment not null primary key',
			'type' => 'int',
			'field' => 'id',
		    },
		    {
			'field' => 'meter',
			'type'  => 'int',
		    },
		    {
			'field' => 'time',
			'type'  => 'int',
		    },

		    {
			'field' => 'reading',
			'type'  => double,
		    },
		    {
			'field' => 'rate',
			'type'  => double,
		    },
		    ],
    };
    return $tablespec;
}


1;
